﻿using System;
using System.Collections.Generic; 
using System.ComponentModel;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.Split;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Date
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityRq0221", Name = "与发送日间隔检证(单位：月)", CategoryType = EnumCategoryType.日期, Model = true)]
    public class VerfityRq0221 : VerfityOneResult
    {
        private int _monthSpan = 6;
        
        [Category("期间间隔,单位:月"), Description("输入间隔值")]
        public int MonthSpan
        {
            get { return _monthSpan; }
            set { _monthSpan = value; }
        }
        
      
        /// <summary>
        /// 第二轮测试 Add
        /// 日期补全
        /// </summary>
        /// <param name="result"></param>
        /// <param name="entryCommon"></param>
        /// <returns></returns>
        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;

            foreach (var rowResult in result)
            { 

                //公历 -> 和历
                var changeRowResult = new DateHelper(ChildImageObject.CategorySetting).ChangeDateStr(rowResult);

                foreach (var colResult in changeRowResult)
                {
                    if (EmptyResultCount(colResult).Equals(1))
                    {
                        continue;
                    }

                    //补全日期
                    var completeData = CompleteDate(colResult);

                    var splitEntryResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, completeData);
                    
                    //每个结果的录入日期
                    DateTime? entryDate = splitEntryResult.GetDateTime();

                    //受番号中的日期
                    DateTime? orderDate = GetOrderDate();

                    //比较
                    if ( entryDate != null && orderDate != null)
                    {
                         

                        bresult = ExceedMonthSpan((DateTime)entryDate, (DateTime)orderDate);
/*                        double  months = monthSpan( (DateTime)entryDate ,(DateTime)orderDate )  ;
 
                        System.Console.WriteLine(string.Format("span month:{0}", months ));

                        if (months >= _monthSpan)
                        {
                            bresult = true;
                        }*/
                    }

                    if (bresult)
                        break;
                    
                }

                if (bresult)
                    break;
            }

            if (bresult)
            {
                System.Console.WriteLine("VerfityRq0221");
            }

            return bresult;
        }


 
        private double monthSpan(DateTime fistDate, DateTime secondDate)

        { 
            
            TimeSpan span = secondDate - fistDate;
            
            double  monthSpan = span.TotalDays / 30.5;
            return monthSpan;
            //return (secondDate.Year - fistDate.Year) * 12 + (secondDate.Month - fistDate.Month);
 
        }


         private bool ExceedMonthSpan(DateTime fistDate, DateTime secondDate)

        {

            DateTime futureTime = fistDate.AddMonths(MonthSpan);

            if (secondDate.CompareTo(futureTime) >0)
            {
                return true;
            }
            else
            {
                return false;
            }
             
 
        }
    }
}
